﻿using DevExpress.Internal;
using DevExpress.Printing.ExportHelpers;
using DevExpress.Xpf.Editors;
using DevExpress.XtraExport.Implementation;
using DevExpress.XtraPrinting.Native.Lines;
using NPOI.OpenXmlFormats.Dml.Chart;
using NPOI.SS.Formula;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FIRFilter
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        Line line1;
        Line line2;
        Line line3;

        /// <summary>
        /// fir滤波参数
        /// </summary>
        //decimal[] fir_parameter = new decimal[] {-7.80533240322441e-05,-3.91913455544067e-05,-4.83282805528452e-05,-5.82982873158762e-05,-6.90027273177729e-05,-8.03108367677942e-05,-9.20584900729133e-05,-0.000104049349864568,-0.000116053779044912,-0.000127808188974885,-0.000139013691530972,-0.000149334403017520,-0.000158402316086318,-0.000165822368871075,-0.000171180813695190,-0.000174052972617947,-0.000174006449238580,-0.000170604512292508,-0.000163407074230685,-0.000151980381383477,-0.000135913534166376,-0.000114832864235318,-8.84116555887713e-05,-5.63660247684305e-05,-1.84543248390861e-05,2.55090637943219e-05,7.56237643414970e-05,0.000131880803459389,0.000194182778047794,0.000262376836924070,0.000336231671491411,0.000415357497441948,0.000499217520291884,0.000587280943710622,0.000678942977504195,0.000773230195882736,0.000869461397091760,0.000966549374335229,0.00106350831935486,0.00115921076029414,0.00125249548497407,0.00134215844595374,0.00142697412015102,0.00150571218583923,0.00157715297523477,0.00164010434197976,0.00169342470345794,0.00173604488925131,0.00176698979694178,0.00178539383093604,0.00179051547808755,0.00178175728696659,0.00175868536909572,0.00172104780212504,0.00166878285116885,0.00160202039755376,0.00152109236960735,0.00142654331492331,0.00131913482482697,0.00119982911093779,0.00106977820724287,0.000930331402339249,0.000783026570089355,0.000629552008650174,0.000471718942207661,0.000311475019414410,0.000150874605098797,-8.00326654897445e-06,-0.000163033450893569,-0.000312051035884636,-0.000453008415150109,-0.000583832959041609,-0.000702621955932085,-0.000807567414780435,-0.000897050177440744,-0.000969642968354736,-0.00102414606212493,-0.00105961107502297,-0.00107536529054006,-0.00107103127582538,-0.00104654081013926,-0.00100214121914421,-0.000938401516241717,-0.000856212622437471,-0.000756782701836365,-0.000641623796576993,-0.000512530912033110,-0.000371563727453672,-0.000221022436827342,-6.34106229920491e-05,9.86052340239447e-05,0.000262235345919728,0.000424599951554164,0.000582784098141328,0.000733893587472060,0.000875097225649141,0.00100367035973081,0.00111705412530584,0.00121290947018998,0.00128914293349688,0.00134395802675198,0.00137591458127948,0.00138392765417760,0.00136730662052494,0.00132580834019396,0.00125958761189786,0.00116927981821779,0.00105593563787459,0.000921057717927443,0.000766560677672171,0.000594759547711690,0.000408333448286640,0.000210287481457062,3.90819469362160e-06,-0.000207286291219762,-0.000419604375610118,-0.000629243494682142,-0.000832357384522568,-0.00102511928287934,-0.00120378994363765,-0.00136479184403383,-0.00150477764513602,-0.00162069243667117,-0.00170983557357291,-0.00176992142206240,-0.00179913411164229,-0.00179616311036190,-0.00176024643201270,-0.00169120119292108,-0.00158943833123535,-0.00145596674040936,-0.00129239815413941,-0.00110093708290041,-0.000884342349225252,-0.000645902495728107,-0.000389397932593805,-0.000119025258137057,0.000160650714926325,0.000444767821790483,0.000728270466804697,0.00100595612938194,0.00127260609105967,0.00152304679465691,0.00175226776742112,0.00195550251335719,0.00212832651313643,0.00226674473431232,0.00236727786989134,0.00242703909399266,0.00244380053281612,0.00241605157809232,0.00234305038957865,0.00222485572228873,0.00206234687035675,0.00185723345041251,0.00161204915976689,0.00133012735695602,0.00101556259049681,0.000673161221575772,0.000308378022519243,-7.27719138079384e-05,-0.000463797832487641,-0.000857836804269995,-0.00124776707508636,-0.00162633285922417,-0.00198626453226150,-0.00232040710428622,-0.00262186190994277,-0.00288410752122683,-0.00310112907096161,-0.00326755208031095,-0.00337874214426907,-0.00343091801619946,-0.00342125219726440,-0.00334793215805913,-0.00321024861692068,-0.00300861762129995,-0.00274462869679894,-0.00242104155961600,-0.00204178107458137,-0.00161190021435993,-0.00113753021960534,-0.000625808399510220,-8.47827472577938e-05,0.000476700112614658,0.00104911988647838,0.00162242958343257,0.00218621420155043,0.00272985853792532,0.00324272372236137,0.00371433330993953,0.00413456194332091,0.00449381966560042,0.00478323377795472,0.00499483466056498,0.00512171724268141,0.00515820109350608,0.00509996887477677,0.00494419476554494,0.00468964125211525,0.00433674083125133,0.00388765862678777,0.00334631121897606,0.00271837462868330,0.00201126373344479,0.00123406747266975,0.000397476567102269,-0.000486328353988435,-0.00140381893869072,-0.00234026503580742,-0.00327993665027824,-0.00420629320120340,-0.00510221625037999,-0.00595023941296365,-0.00673280411099375,-0.00743251296579633,-0.00803239064333218,-0.00851614876432161,-0.00886844429991518,-0.00907512697706558,-0.00912348102967734,-0.00900244993618241,-0.00870284107723165,-0.00821750627943594,-0.00754150065923610,-0.00667221269015073,-0.00560945974726041,-0.00435555237329664,-0.00291533145836267,-0.00129615617187117,0.000492129476421742,0.00243727717704418,0.00452473734067539,0.00673781218475032,0.00905782776973246,0.0114643360542234,0.0139353586867116,0.0164476376064677,0.0189769151984461,0.0214982366118438,0.0239862497111346,0.0264155306480807,0.0287609061147417,0.0309977671478851,0.0331023990658771,0.0350522780514061,0.0368263728816851,0.0384054135248993,0.0397721528006051,0.0409115892287292,0.0418111654555516,0.0424609378262038,0.0428537083770091,0.0429851183724817,0.0428537083770091,0.0424609378262038,0.0418111654555516,0.0409115892287292,0.0397721528006051,0.0384054135248993,0.0368263728816851,0.0350522780514061,0.0331023990658771,0.0309977671478851,0.0287609061147417,0.0264155306480807,0.0239862497111346,0.0214982366118438,0.0189769151984461,0.0164476376064677,0.0139353586867116,0.0114643360542234,0.00905782776973246,0.00673781218475032,0.00452473734067539,0.00243727717704418,0.000492129476421742,-0.00129615617187117,-0.00291533145836267,-0.00435555237329664,-0.00560945974726041,-0.00667221269015073,-0.00754150065923610,-0.00821750627943594,-0.00870284107723165,-0.00900244993618241,-0.00912348102967734,-0.00907512697706558,-0.00886844429991518,-0.00851614876432161,-0.00803239064333218,-0.00743251296579633,-0.00673280411099375,-0.00595023941296365,-0.00510221625037999,-0.00420629320120340,-0.00327993665027824,-0.00234026503580742,-0.00140381893869072,-0.000486328353988435,0.000397476567102269,0.00123406747266975,0.00201126373344479,0.00271837462868330,0.00334631121897606,0.00388765862678777,0.00433674083125133,0.00468964125211525,0.00494419476554494,0.00509996887477677,0.00515820109350608,0.00512171724268141,0.00499483466056498,0.00478323377795472,0.00449381966560042,0.00413456194332091,0.00371433330993953,0.00324272372236137,0.00272985853792532,0.00218621420155043,0.00162242958343257,0.00104911988647838,0.000476700112614658,-8.47827472577938e-05,-0.000625808399510220,-0.00113753021960534,-0.00161190021435993,-0.00204178107458137,-0.00242104155961600,-0.00274462869679894,-0.00300861762129995,-0.00321024861692068,-0.00334793215805913,-0.00342125219726440,-0.00343091801619946,-0.00337874214426907,-0.00326755208031095,-0.00310112907096161,-0.00288410752122683,-0.00262186190994277,-0.00232040710428622,-0.00198626453226150,-0.00162633285922417,-0.00124776707508636,-0.000857836804269995,-0.000463797832487641,-7.27719138079384e-05,0.000308378022519243,0.000673161221575772,0.00101556259049681,0.00133012735695602,0.00161204915976689,0.00185723345041251,0.00206234687035675,0.00222485572228873,0.00234305038957865,0.00241605157809232,0.00244380053281612,0.00242703909399266,0.00236727786989134,0.00226674473431232,0.00212832651313643,0.00195550251335719,0.00175226776742112,0.00152304679465691,0.00127260609105967,0.00100595612938194,0.000728270466804697,0.000444767821790483,0.000160650714926325,-0.000119025258137057,-0.000389397932593805,-0.000645902495728107,-0.000884342349225252,-0.00110093708290041,-0.00129239815413941,-0.00145596674040936,-0.00158943833123535,-0.00169120119292108,-0.00176024643201270,-0.00179616311036190,-0.00179913411164229,-0.00176992142206240,-0.00170983557357291,-0.00162069243667117,-0.00150477764513602,-0.00136479184403383,-0.00120378994363765,-0.00102511928287934,-0.000832357384522568,-0.000629243494682142,-0.000419604375610118,-0.000207286291219762,3.90819469362160e-06,0.000210287481457062,0.000408333448286640,0.000594759547711690,0.000766560677672171,0.000921057717927443,0.00105593563787459,0.00116927981821779,0.00125958761189786,0.00132580834019396,0.00136730662052494,0.00138392765417760,0.00137591458127948,0.00134395802675198,0.00128914293349688,0.00121290947018998,0.00111705412530584,0.00100367035973081,0.000875097225649141,0.000733893587472060,0.000582784098141328,0.000424599951554164,0.000262235345919728,9.86052340239447e-05,-6.34106229920491e-05,-0.000221022436827342,-0.000371563727453672,-0.000512530912033110,-0.000641623796576993,-0.000756782701836365,-0.000856212622437471,-0.000938401516241717,-0.00100214121914421,-0.00104654081013926,-0.00107103127582538,-0.00107536529054006,-0.00105961107502297,-0.00102414606212493,-0.000969642968354736,-0.000897050177440744,-0.000807567414780435,-0.000702621955932085,-0.000583832959041609,-0.000453008415150109,-0.000312051035884636,-0.000163033450893569,-8.00326654897445e-06,0.000150874605098797,0.000311475019414410,0.000471718942207661,0.000629552008650174,0.000783026570089355,0.000930331402339249,0.00106977820724287,0.00119982911093779,0.00131913482482697,0.00142654331492331,0.00152109236960735,0.00160202039755376,0.00166878285116885,0.00172104780212504,0.00175868536909572,0.00178175728696659,0.00179051547808755,0.00178539383093604,0.00176698979694178,0.00173604488925131,0.00169342470345794,0.00164010434197976,0.00157715297523477,0.00150571218583923,0.00142697412015102,0.00134215844595374,0.00125249548497407,0.00115921076029414,0.00106350831935486,0.000966549374335229,0.000869461397091760,0.000773230195882736,0.000678942977504195,0.000587280943710622,0.000499217520291884,0.000415357497441948,0.000336231671491411,0.000262376836924070,0.000194182778047794,0.000131880803459389,7.56237643414970e-05,2.55090637943219e-05,-1.84543248390861e-05,-5.63660247684305e-05,-8.84116555887713e-05,-0.000114832864235318,-0.000135913534166376,-0.000151980381383477,-0.000163407074230685,-0.000170604512292508,-0.000174006449238580,-0.000174052972617947,-0.000171180813695190,-0.000165822368871075,-0.000158402316086318,-0.000149334403017520,-0.000139013691530972,-0.000127808188974885,-0.000116053779044912,-0.000104049349864568,-9.20584900729133e-05,-8.03108367677942e-05,-6.90027273177729e-05,-5.82982873158762e-05,-4.83282805528452e-05,-3.91913455544067e-05,-7.80533240322441e-05};
        decimal[] fir_parameter = new decimal[] { -7.80533240322441e-05M, -3.91913455544067e-05M, -4.83282805528452e-05M, -5.82982873158762e-05M, -6.90027273177729e-05M, -8.03108367677942e-05M, -9.20584900729133e-05M, -0.000104049349864568M, -0.000116053779044912M, -0.000127808188974885M, -0.000139013691530972M, -0.000149334403017520M, -0.000158402316086318M, -0.000165822368871075M, -0.000171180813695190M, -0.000174052972617947M, -0.000174006449238580M, -0.000170604512292508M, -0.000163407074230685M, -0.000151980381383477M, -0.000135913534166376M, -0.000114832864235318M, -8.84116555887713e-05M, -5.63660247684305e-05M, -1.84543248390861e-05M, 2.55090637943219e-05M, 7.56237643414970e-05M, 0.000131880803459389M, 0.000194182778047794M, 0.000262376836924070M, 0.000336231671491411M, 0.000415357497441948M, 0.000499217520291884M, 0.000587280943710622M, 0.000678942977504195M, 0.000773230195882736M, 0.000869461397091760M, 0.000966549374335229M, 0.00106350831935486M, 0.00115921076029414M, 0.00125249548497407M, 0.00134215844595374M, 0.00142697412015102M, 0.00150571218583923M, 0.00157715297523477M, 0.00164010434197976M, 0.00169342470345794M, 0.00173604488925131M, 0.00176698979694178M, 0.00178539383093604M, 0.00179051547808755M, 0.00178175728696659M, 0.00175868536909572M, 0.00172104780212504M, 0.00166878285116885M, 0.00160202039755376M, 0.00152109236960735M, 0.00142654331492331M, 0.00131913482482697M, 0.00119982911093779M, 0.00106977820724287M, 0.000930331402339249M, 0.000783026570089355M, 0.000629552008650174M, 0.000471718942207661M, 0.000311475019414410M, 0.000150874605098797M, -8.00326654897445e-06M, -0.000163033450893569M, -0.000312051035884636M, -0.000453008415150109M, -0.000583832959041609M, -0.000702621955932085M, -0.000807567414780435M, -0.000897050177440744M, -0.000969642968354736M, -0.00102414606212493M, -0.00105961107502297M, -0.00107536529054006M, -0.00107103127582538M, -0.00104654081013926M, -0.00100214121914421M, -0.000938401516241717M, -0.000856212622437471M, -0.000756782701836365M, -0.000641623796576993M, -0.000512530912033110M, -0.000371563727453672M, -0.000221022436827342M, -6.34106229920491e-05M, 9.86052340239447e-05M, 0.000262235345919728M, 0.000424599951554164M, 0.000582784098141328M, 0.000733893587472060M, 0.000875097225649141M, 0.00100367035973081M, 0.00111705412530584M, 0.00121290947018998M, 0.00128914293349688M, 0.00134395802675198M, 0.00137591458127948M, 0.00138392765417760M, 0.00136730662052494M, 0.00132580834019396M, 0.00125958761189786M, 0.00116927981821779M, 0.00105593563787459M, 0.000921057717927443M, 0.000766560677672171M, 0.000594759547711690M, 0.000408333448286640M, 0.000210287481457062M, 3.90819469362160e-06M, -0.000207286291219762M, -0.000419604375610118M, -0.000629243494682142M, -0.000832357384522568M, -0.00102511928287934M, -0.00120378994363765M, -0.00136479184403383M, -0.00150477764513602M, -0.00162069243667117M, -0.00170983557357291M, -0.00176992142206240M, -0.00179913411164229M, -0.00179616311036190M, -0.00176024643201270M, -0.00169120119292108M, -0.00158943833123535M, -0.00145596674040936M, -0.00129239815413941M, -0.00110093708290041M, -0.000884342349225252M, -0.000645902495728107M, -0.000389397932593805M, -0.000119025258137057M, 0.000160650714926325M, 0.000444767821790483M, 0.000728270466804697M, 0.00100595612938194M, 0.00127260609105967M, 0.00152304679465691M, 0.00175226776742112M, 0.00195550251335719M, 0.00212832651313643M, 0.00226674473431232M, 0.00236727786989134M, 0.00242703909399266M, 0.00244380053281612M, 0.00241605157809232M, 0.00234305038957865M, 0.00222485572228873M, 0.00206234687035675M, 0.00185723345041251M, 0.00161204915976689M, 0.00133012735695602M, 0.00101556259049681M, 0.000673161221575772M, 0.000308378022519243M, -7.27719138079384e-05M, -0.000463797832487641M, -0.000857836804269995M, -0.00124776707508636M, -0.00162633285922417M, -0.00198626453226150M, -0.00232040710428622M, -0.00262186190994277M, -0.00288410752122683M, -0.00310112907096161M, -0.00326755208031095M, -0.00337874214426907M, -0.00343091801619946M, -0.00342125219726440M, -0.00334793215805913M, -0.00321024861692068M, -0.00300861762129995M, -0.00274462869679894M, -0.00242104155961600M, -0.00204178107458137M, -0.00161190021435993M, -0.00113753021960534M, -0.000625808399510220M, -8.47827472577938e-05M, 0.000476700112614658M, 0.00104911988647838M, 0.00162242958343257M, 0.00218621420155043M, 0.00272985853792532M, 0.00324272372236137M, 0.00371433330993953M, 0.00413456194332091M, 0.00449381966560042M, 0.00478323377795472M, 0.00499483466056498M, 0.00512171724268141M, 0.00515820109350608M, 0.00509996887477677M, 0.00494419476554494M, 0.00468964125211525M, 0.00433674083125133M, 0.00388765862678777M, 0.00334631121897606M, 0.00271837462868330M, 0.00201126373344479M, 0.00123406747266975M, 0.000397476567102269M, -0.000486328353988435M, -0.00140381893869072M, -0.00234026503580742M, -0.00327993665027824M, -0.00420629320120340M, -0.00510221625037999M, -0.00595023941296365M, -0.00673280411099375M, -0.00743251296579633M, -0.00803239064333218M, -0.00851614876432161M, -0.00886844429991518M, -0.00907512697706558M, -0.00912348102967734M, -0.00900244993618241M, -0.00870284107723165M, -0.00821750627943594M, -0.00754150065923610M, -0.00667221269015073M, -0.00560945974726041M, -0.00435555237329664M, -0.00291533145836267M, -0.00129615617187117M, 0.000492129476421742M, 0.00243727717704418M, 0.00452473734067539M, 0.00673781218475032M, 0.00905782776973246M, 0.0114643360542234M, 0.0139353586867116M, 0.0164476376064677M, 0.0189769151984461M, 0.0214982366118438M, 0.0239862497111346M, 0.0264155306480807M, 0.0287609061147417M, 0.0309977671478851M, 0.0331023990658771M, 0.0350522780514061M, 0.0368263728816851M, 0.0384054135248993M, 0.0397721528006051M, 0.0409115892287292M, 0.0418111654555516M, 0.0424609378262038M, 0.0428537083770091M, 0.0429851183724817M, 0.0428537083770091M, 0.0424609378262038M, 0.0418111654555516M, 0.0409115892287292M, 0.0397721528006051M, 0.0384054135248993M, 0.0368263728816851M, 0.0350522780514061M, 0.0331023990658771M, 0.0309977671478851M, 0.0287609061147417M, 0.0264155306480807M, 0.0239862497111346M, 0.0214982366118438M, 0.0189769151984461M, 0.0164476376064677M, 0.0139353586867116M, 0.0114643360542234M, 0.00905782776973246M, 0.00673781218475032M, 0.00452473734067539M, 0.00243727717704418M, 0.000492129476421742M, -0.00129615617187117M, -0.00291533145836267M, -0.00435555237329664M, -0.00560945974726041M, -0.00667221269015073M, -0.00754150065923610M, -0.00821750627943594M, -0.00870284107723165M, -0.00900244993618241M, -0.00912348102967734M, -0.00907512697706558M, -0.00886844429991518M, -0.00851614876432161M, -0.00803239064333218M, -0.00743251296579633M, -0.00673280411099375M, -0.00595023941296365M, -0.00510221625037999M, -0.00420629320120340M, -0.00327993665027824M, -0.00234026503580742M, -0.00140381893869072M, -0.000486328353988435M, 0.000397476567102269M, 0.00123406747266975M, 0.00201126373344479M, 0.00271837462868330M, 0.00334631121897606M, 0.00388765862678777M, 0.00433674083125133M, 0.00468964125211525M, 0.00494419476554494M, 0.00509996887477677M, 0.00515820109350608M, 0.00512171724268141M, 0.00499483466056498M, 0.00478323377795472M, 0.00449381966560042M, 0.00413456194332091M, 0.00371433330993953M, 0.00324272372236137M, 0.00272985853792532M, 0.00218621420155043M, 0.00162242958343257M, 0.00104911988647838M, 0.000476700112614658M, -8.47827472577938e-05M, -0.000625808399510220M, -0.00113753021960534M, -0.00161190021435993M, -0.00204178107458137M, -0.00242104155961600M, -0.00274462869679894M, -0.00300861762129995M, -0.00321024861692068M, -0.00334793215805913M, -0.00342125219726440M, -0.00343091801619946M, -0.00337874214426907M, -0.00326755208031095M, -0.00310112907096161M, -0.00288410752122683M, -0.00262186190994277M, -0.00232040710428622M, -0.00198626453226150M, -0.00162633285922417M, -0.00124776707508636M, -0.000857836804269995M, -0.000463797832487641M, -7.27719138079384e-05M, 0.000308378022519243M, 0.000673161221575772M, 0.00101556259049681M, 0.00133012735695602M, 0.00161204915976689M, 0.00185723345041251M, 0.00206234687035675M, 0.00222485572228873M, 0.00234305038957865M, 0.00241605157809232M, 0.00244380053281612M, 0.00242703909399266M, 0.00236727786989134M, 0.00226674473431232M, 0.00212832651313643M, 0.00195550251335719M, 0.00175226776742112M, 0.00152304679465691M, 0.00127260609105967M, 0.00100595612938194M, 0.000728270466804697M, 0.000444767821790483M, 0.000160650714926325M, -0.000119025258137057M, -0.000389397932593805M, -0.000645902495728107M, -0.000884342349225252M, -0.00110093708290041M, -0.00129239815413941M, -0.00145596674040936M, -0.00158943833123535M, -0.00169120119292108M, -0.00176024643201270M, -0.00179616311036190M, -0.00179913411164229M, -0.00176992142206240M, -0.00170983557357291M, -0.00162069243667117M, -0.00150477764513602M, -0.00136479184403383M, -0.00120378994363765M, -0.00102511928287934M, -0.000832357384522568M, -0.000629243494682142M, -0.000419604375610118M, -0.000207286291219762M, 3.90819469362160e-06M, 0.000210287481457062M, 0.000408333448286640M, 0.000594759547711690M, 0.000766560677672171M, 0.000921057717927443M, 0.00105593563787459M, 0.00116927981821779M, 0.00125958761189786M, 0.00132580834019396M, 0.00136730662052494M, 0.00138392765417760M, 0.00137591458127948M, 0.00134395802675198M, 0.00128914293349688M, 0.00121290947018998M, 0.00111705412530584M, 0.00100367035973081M, 0.000875097225649141M, 0.000733893587472060M, 0.000582784098141328M, 0.000424599951554164M, 0.000262235345919728M, 9.86052340239447e-05M, -6.34106229920491e-05M, -0.000221022436827342M, -0.000371563727453672M, -0.000512530912033110M, -0.000641623796576993M, -0.000756782701836365M, -0.000856212622437471M, -0.000938401516241717M, -0.00100214121914421M, -0.00104654081013926M, -0.00107103127582538M, -0.00107536529054006M, -0.00105961107502297M, -0.00102414606212493M, -0.000969642968354736M, -0.000897050177440744M, -0.000807567414780435M, -0.000702621955932085M, -0.000583832959041609M, -0.000453008415150109M, -0.000312051035884636M, -0.000163033450893569M, -8.00326654897445e-06M, 0.000150874605098797M, 0.000311475019414410M, 0.000471718942207661M, 0.000629552008650174M, 0.000783026570089355M, 0.000930331402339249M, 0.00106977820724287M, 0.00119982911093779M, 0.00131913482482697M, 0.00142654331492331M, 0.00152109236960735M, 0.00160202039755376M, 0.00166878285116885M, 0.00172104780212504M, 0.00175868536909572M, 0.00178175728696659M, 0.00179051547808755M, 0.00178539383093604M, 0.00176698979694178M, 0.00173604488925131M, 0.00169342470345794M, 0.00164010434197976M, 0.00157715297523477M, 0.00150571218583923M, 0.00142697412015102M, 0.00134215844595374M, 0.00125249548497407M, 0.00115921076029414M, 0.00106350831935486M, 0.000966549374335229M, 0.000869461397091760M, 0.000773230195882736M, 0.000678942977504195M, 0.000587280943710622M, 0.000499217520291884M, 0.000415357497441948M, 0.000336231671491411M, 0.000262376836924070M, 0.000194182778047794M, 0.000131880803459389M, 7.56237643414970e-05M, 2.55090637943219e-05M, -1.84543248390861e-05M, -5.63660247684305e-05M, -8.84116555887713e-05M, -0.000114832864235318M, -0.000135913534166376M, -0.000151980381383477M, -0.000163407074230685M, -0.000170604512292508M, -0.000174006449238580M, -0.000174052972617947M, -0.000171180813695190M, -0.000165822368871075M, -0.000158402316086318M, -0.000149334403017520M, -0.000139013691530972M, -0.000127808188974885M, -0.000116053779044912M, -0.000104049349864568M, -9.20584900729133e-05M, -8.03108367677942e-05M, -6.90027273177729e-05M, -5.82982873158762e-05M, -4.83282805528452e-05M, -3.91913455544067e-05M, -7.80533240322441e-05M };

        //decimal average = 1.0697M;
        decimal average = 1.06965975835748854590M;
        /// <summary>
        /// excel读取的数据
        /// </summary>
        decimal[] origin_data = null;
        decimal[] fir_data = null;
        decimal S = 1;

        decimal[] result_data = null;


        public List<CustomElement> SourceCollection1 { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            this.btnLow.IsEnabled = false;
            this.btnExport.IsEnabled = false;
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            int len = origin_data.Length;
            //pTestHistory_devspkFrameData1.EditValue = DoubleToDisplay(origin_data);

            firFiltter();

            //pTestHistory_devspkFrameData2.EditValue = DoubleToDisplay(fir_data);



        }
        public List<CustomElement> DoubleToDisplay(double[] source)
        {
            //CustomElement中的ArgumentColumn和ValueColumn通过前台绑定了，只需要传入CustomElement相关的数据即可。
            List<CustomElement> listR = new List<CustomElement>();
            for (int i = 0; i < source.Length; i++)
            {
                listR.Add(new CustomElement()
                {
                    ArgumentColumn = i,
                    ValueColumn = source[i]
                });
            }
            return listR;
        }

        public void firFiltter()
        {
            fir_data = new decimal[origin_data.Length-507];
            for(int i = 0; i < origin_data.Length - 507; i++)
            {
                for(int j = 0; j < 507; j++)
                {
                    fir_data[i] += origin_data[i+j]*fir_parameter[j];
                }
                fir_data[i] = fir_data[i] / average;
            }
        }

        private void btnLow_Click(object sender, RoutedEventArgs e)
        {
            if (origin_data.Length < 507)
            {
                string messageBoxText = "原始数据少于507条，无法进行转换";
                string caption = "提示";
                MessageBoxButton button = MessageBoxButton.OK;
                MessageBoxImage icon = MessageBoxImage.Warning;
                MessageBoxResult result;
                result = MessageBox.Show(messageBoxText, caption, button, icon, MessageBoxResult.OK);
                return;
            }

            firFiltter();

            //pTestHistory_devspkFrameData2.EditValue = DoubleToDisplay(fir_data);

            result_data = new decimal[fir_data.Length];
            for(int i = 0; i < fir_data.Length; i++)
            {
                result_data[i] = (origin_data[258 + i] - fir_data[i]) / S * 1000;
            }
            //pTestHistory_devspkFrameData3.EditValue = DoubleToDisplay(result_data);
            this.btnExport.IsEnabled = true;
        }


        private void btnReadFile_Click(object sender, RoutedEventArgs e)
        {
            string fileName = SelectFileWpf();
            this.btnExport.IsEnabled = false;
            this.btnLow.IsEnabled = false;
            this.txtFileName.Content = "文件路径：" + fileName;
            ISheet sheet = null;
            int startRow = 0;
            FileStream fs = null;
            IWorkbook workbook = null;
            try
            {
                fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                workbook = WorkbookFactory.Create(fs);
                sheet = workbook.GetSheetAt(0);
                if (sheet != null)
                {
                    int rowCount = sheet.LastRowNum + 1;
                    origin_data = new decimal[rowCount];
                    for (int i = startRow; i <= rowCount; i++)
                    {
                        IRow row = sheet.GetRow(i);
                        if (row != null)
                        {
                            if (row.GetCell(0) != null)
                            {
                                origin_data[i] = (decimal)row.GetCell(0).NumericCellValue;
                            }
                        }
                    }
                }
                fs.Close();
                workbook.Close();
                //pTestHistory_devspkFrameData1.EditValue = DoubleToDisplay(origin_data);
                // 设置控件可用
                this.btnLow.IsEnabled = true;
            }
            catch (Exception ex)
            {
                this.txtFileName.Content = "数据格式错误";
            }
        }

        public string SelectFileWpf()
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog()
            {
                Filter = "Excel |*.xls;*.xlsx"
            };
            var result = openFileDialog.ShowDialog();
            if (result == true)
            {
                return openFileDialog.FileName;
            }
            else
            {
                return null;
            }
        }



        private void Border_MouseLeave(object sender, MouseEventArgs e)
        {
            Grid x = sender as Grid;
            x.Children.Remove(line2);
        }

        private void Border_MouseMove(object sender, MouseEventArgs e)
        {
            if (true)
            {
                Grid xx = (Grid)(sender as Border).Parent;
                if (line2 != null)
                {
                    xx.Children.Remove(line1);
                    xx.Children.Remove(line2);
                }
                Point p = e.GetPosition(xx);
                line1 = new Line();
                line2 = new Line();
                line2.Stroke = Brushes.Blue;
                line2.StrokeThickness = 1;
                line2.X1 = p.X;
                line2.X2 = p.X;
                line2.Y1 = 0;
                line2.Y2 = xx.ActualHeight;
                line2.SnapsToDevicePixels = true;
                xx.Children.Add(line2);

                List<SparklineEdit> list = FindVisualChild<SparklineEdit>(xx);
                if (list[0].EditValue != null)
                {
                    CustomElement re = LabelDisplay(list[0], e, SourceCollection1);
                    label2.Dispatcher.Invoke(new Action(() => label2.Content = " 当前值：" + re.ArgumentColumn + "," + re.ValueColumn));
                    Console.WriteLine(label2.Content);
                }
            }
        }
        public CustomElement LabelDisplay(SparklineEdit sked, MouseEventArgs e, List<CustomElement> SourceCollectionTest)
        {
            SourceCollectionTest = (List<CustomElement>)sked.EditValue;
            if (SourceCollectionTest.Count != 0)
            {
                Point p = e.GetPosition((IInputElement)sked);
                double xxx = p.X / (sked.ActualWidth / (SourceCollectionTest.Count - 1));
                if ((xxx - (int)xxx) > 0.5)
                {
                    //label.Content = $"当前值：{((CustomElement)SourceCollectionTest[((int)xxx + 1)]).ValueColumn}";
                    return (SourceCollectionTest[((int)xxx + 1)]);
                }
                else
                {
                    //label.Content = $"当前值：{((CustomElement)SourceCollectionTest[(int)xxx]).ValueColumn}";
                    return (SourceCollectionTest[((int)xxx)]);
                }
            }
            return new CustomElement();
        }

        public static List<T> FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
        {
            try
            {
                List<T> TList = new List<T> { };
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                    if (child != null && child is T)
                    {
                        TList.Add((T)child);
                    }
                    else
                    {
                        List<T> childOfChildren = FindVisualChild<T>(child);
                        if (childOfChildren != null)
                        {
                            TList.AddRange(childOfChildren);
                        }
                    }
                }
                return TList;
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
                return null;
            }
        }

        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            if (S == null)
            {
                string messageBoxText = "请输入参数";
                string caption = "提示";
                MessageBoxButton button = MessageBoxButton.OK;
                MessageBoxImage icon = MessageBoxImage.Warning;
                MessageBoxResult result;
                result = MessageBox.Show(messageBoxText, caption, button, icon, MessageBoxResult.OK);
                return;
            }
            ISheet sheet = null;
            IWorkbook workbook = null;
            string path = this.txtFileName.Content.ToString().Substring(5);
            try
            {
                using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    workbook = WorkbookFactory.Create(fs);
                    fs.Close();
                }
                sheet = workbook.GetSheetAt(0);
                if (sheet != null)
                {
                    int len = result_data.Length;
                    for (int i = 0; i < len; i++)
                    {
                        //double num = origin_data[258 + i] - fir_data[i];
                        sheet.GetRow(258 + i).CreateCell(1).SetCellValue((double)result_data[i]);
                    }
                }
                using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    workbook.Write(fs);
                    fs.Close();
                }

                workbook.Close();
                System.Diagnostics.Process.Start(System.IO.Path.GetFullPath(path));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void txtS_TextChanged(object sender, TextChangedEventArgs e)
        {
            string input = (sender as TextBox).Text; //1234567

            if (Regex.IsMatch(input, "^(\\-|\\+)?\\d+(\\.\\d+)?$"))
            {
                S = (decimal)Convert.ToSingle(input);
                (sender as TextBox).Background = new SolidColorBrush(Colors.White);
            }
            else
            {
                (sender as TextBox).Background = new SolidColorBrush(Colors.Red);
            }
        }
    }


    public class CustomElement
    {
        public int ArgumentColumn { get; set; }
        public double ValueColumn { get; set; }
    }
}
